OneHot ================= 根据输入的索引值,在指定轴上生成独热编码(one-hot encoding)。 .. math:: output_{i_1, i_2, \dots, i_{axis}, \dots, i_n} = \begin{cases} on\_value, & \text{if } i_{axis} = indices_{j} \\ off\_value, & \text{otherwise} \end{cases} 其中,:math:`indices_{j}` 为输入索引张量的元素,索引 :math:`j` 与输出下标 :math:`(i_1, \dots, i_{axis-1}, i_{axis+1}, \dots, i_n)` 一一对应。 :math:`depth` 表示 one-hot 向量的长度,:math:`axis` 表示新维度插入的位置。 当启用 ``support_neg_index`` 时,若 :math:`indices_{j} < 0`,则执行如下修正: .. math:: indices_{j} = indices_{j} + depth 输入: - **on_off** - 包含 **on_value** 和 **off_value** 的数组地址。对于标量数据类型,数组长度为 2,依次为 ``[on_value, off_value]``;对于复数类型(c64/c128),数组长度为 4,依次为 ``[on_real, on_imag, off_real, off_imag]``。 - **indices** - 输入索引值地址,索引元素类型为 ``int``。 - **params** - 参数数组地址,依次存放以下 5 个元素: - ``params[0]``:``axis``,指定生成 one-hot 编码的轴。 - ``params[1]``:``depth``,独热编码向量的长度。 - ``params[2]``:``support_neg_index``,是否支持负索引,取值为 0(不支持)或 1(支持)。 - ``params[3]``:``indices_shape_size``,输入索引张量的维度数(即 ``indices_shape`` 数组的长度)。 - ``params[4]``:``indices_shape``,输入索引张量各维度大小的数组地址。 - **core_mask** - 核心掩码,指定使用的计算核心(仅共享存储版本需要)。 输出: - **output** - 生成的独热编码结果地址。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持int8, int16, int32, fp32, fp64, cplx64, cplx128 - MT7004 支持fp16, fp32, int16, int32, cplx64 - 本算子通过 ``params`` 数组将 ``axis``、``depth``、``support_neg_index``、``indices_shape_size``、``indices_shape`` 打包传入。 **共享存储版本:** .. c:function:: void i8_one_hot_s(int8_t *on_off, int *indices, int8_t *output, long long *params, int core_mask) .. c:function:: void i16_one_hot_s(int16_t *on_off, int *indices, int16_t *output, long long *params, int core_mask) .. c:function:: void i32_one_hot_s(int *on_off, int *indices, int *output, long long *params, int core_mask) .. c:function:: void hp_one_hot_s(half *on_off, int *indices, half *output, long long *params, int core_mask) .. c:function:: void fp_one_hot_s(float *on_off, int *indices, float *output, long long *params, int core_mask) .. c:function:: void dp_one_hot_s(double *on_off, int *indices, double *output, long long *params, int core_mask) .. c:function:: void c64_one_hot_s(float* on_off, int *indices, float *output, long long *params, int core_mask) .. c:function:: void c128_one_hot_s(double* on_off, int *indices, double *output, long long *params, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 13 //MT7004示例 #include int main(int argc, char* argv[]) { float on_off[2] = {1.0, 0.0}; int axis=1, depth=4, indices_shape_size=2; int support_neg_index=1; int indices_shape[2] = {3, 3}; long long params[5] = {axis, depth, support_neg_index, indices_shape_size, (long long)indices_shape}; int *indices = (int *)0xA0000000; // indices在DDR空间 float *output = (float*)0xB0000000; // indices_shape[0] * depth * indices_shape[1] int core_mask = 0xff; fp_one_hot_s(on_off, indices, output, params, core_mask); return 0; } **私有存储版本:** .. c:function:: void i8_one_hot_p(int8_t *on_off, int *indices, int8_t *output, long long *params) .. c:function:: void i16_one_hot_p(int16_t *on_off, int *indices, int16_t *output, long long *params) .. c:function:: void i32_one_hot_p(int *on_off, int *indices, int *output, long long *params) .. c:function:: void hp_one_hot_p(half *on_off, int *indices, half *output, long long *params) .. c:function:: void fp_one_hot_p(float *on_off, int *indices, float *output, long long *params) .. c:function:: void dp_one_hot_p(double *on_off, int *indices, double *output, long long *params) .. c:function:: void c64_one_hot_p(float* on_off, int *indices, float *output, long long *params) .. c:function:: void c128_one_hot_p(double* on_off, int *indices, double *output, long long *params) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 12 //MT7004示例 #include int main(int argc, char* argv[]) { float on_off[2] = {1.0, 0.0}; int axis=1, depth=4, indices_shape_size=2; int support_neg_index=1; int indices_shape[2] = {3, 3}; long long params[5] = {axis, depth, support_neg_index, indices_shape_size, (long long)indices_shape}; int *indices = (int *)0x10000000; // indices在L2空间 float *output = (float*)0x10001000; // indices_shape[0] * depth * indices_shape[1] fp_one_hot_p(on_off, indices, output, params); return 0; }